#event study functions----------------------------------------------------------------------
make_twfe_event_study <- function(df, govt_prop, pop_prop, occ_filter, lab1, lab2){
  var_govt_prop = enquo(govt_prop)
  var_pop_prop = enquo(pop_prop)
  
  df <-
    df %>%
    filter_(occ_filter) %>%
    mutate(
      #outcome variables
      govt_prop = !!var_govt_prop,
      pop_prop = !!var_pop_prop,
      over_prop = govt_prop - pop_prop,
      
      #independent variables    
      ever_treat = ((!is.na(treat))*1),
      distance = case_when(ever_treat == 0 ~ 0,
                           TRUE ~ as.numeric(YEAR) - as.numeric(all)),
      grouped_distance = floor(distance/10))
  
  feols(govt_prop ~ i(grouped_distance, ever_treat, ref = -1) + pop_prop | city + YEAR, cluster = ~city, data = df) %>%
    tidy() %>%
    select(term, estimate, std.error) %>%
    add_row(term = "-1",  estimate = 0, std.error = NA_real_) %>%
    mutate(val = str_remove_all(term, "[:alpha:]|\\_|\\:"),
           lab1 = lab1,
           lab2 = lab2)
}

regional_df <- 
  analysis_df_3 %>%
  mutate(state = str_remove(city, '.*, '),
         region = case_when(state %in% c('pa', 'nj', 'ny', 'ct', 'ri', 'ma', 'nh', 'vt', 'me') ~ 'north',
                            state %in% c('tx', 'ok', 'ar', 'la', 'ms', 'al', 'tn', 'ky', 'wv', 'md', 'de', 'va', 'nc', 'sc', 'ga', 'fl') ~ 'south',
                            state %in% c('wa', 'or', 'id', 'mt', 'wy', 'ca', 'nv', 'ut', 'co', 'az', 'nm') ~ 'west',
                            state %in% c('nd', 'sd', 'ne', 'ks', 'mn', 'ia', 'mo', 'wi', 'il', 'in', 'mi', 'oh') ~ 'midwest'))


bc_out1_no <- make_twfe_event_study(regional_df %>% filter(region == "north"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = "Blue Collar") %>% mutate(reg = "North")
bc_out2_no <- make_twfe_event_study(regional_df%>% filter(region == "north"), govt_white_x_native_born, white_x_native_born, 'occ == "blue_collar"', lab1 = "White Native Born", lab2 = "Blue Collar") %>% mutate(reg = "North")
bc_out3_no <- make_twfe_event_study(regional_df%>% filter(region == "north"), govt_black_x_native_born, black_x_native_born, 'occ == "blue_collar"', lab1 = "Black Native Born", lab2 = "Blue Collar") %>% mutate(reg = "North")

bc_out1_so <- make_twfe_event_study(regional_df %>% filter(region == "south"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = "Blue Collar") %>% mutate(reg = "South")
bc_out2_so <- make_twfe_event_study(regional_df%>% filter(region == "south"), govt_white_x_native_born, white_x_native_born, 'occ == "blue_collar"', lab1 = "White Native Born", lab2 = "Blue Collar") %>% mutate(reg = "South")
bc_out3_so <- make_twfe_event_study(regional_df%>% filter(region == "south"), govt_black_x_native_born, black_x_native_born, 'occ == "blue_collar"', lab1 = "Black Native Born", lab2 = "Blue Collar") %>% mutate(reg = "South")

bc_out1_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = "Blue Collar") %>% mutate(reg = "Midwest")
bc_out2_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_white_x_native_born, white_x_native_born, 'occ == "blue_collar"', lab1 = "White Native Born", lab2 = "Blue Collar") %>% mutate(reg = "Midwest")
bc_out3_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_black_x_native_born, black_x_native_born, 'occ == "blue_collar"', lab1 = "Black Native Born", lab2 = "Blue Collar") %>% mutate(reg = "Midwest")

bc_out1_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = "Blue Collar") %>% mutate(reg = "West")
bc_out2_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_white_x_native_born, white_x_native_born, 'occ == "blue_collar"', lab1 = "White Native Born", lab2 = "Blue Collar") %>% mutate(reg = "West")
bc_out3_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_black_x_native_born, black_x_native_born, 'occ == "blue_collar"', lab1 = "Black Native Born", lab2 = "Blue Collar") %>% mutate(reg = "West")

#white collar----------------------------------------------------------------------
wc_out1_no <- make_twfe_event_study(regional_df %>% filter(region == "north"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "white_collar"', lab1 = "White Foreign Born", lab2 = "White Collar") %>% mutate(reg = "North")
wc_out2_no <- make_twfe_event_study(regional_df%>% filter(region == "north"), govt_white_x_native_born, white_x_native_born, 'occ == "white_collar"', lab1 = "White Native Born", lab2 = "White Collar") %>% mutate(reg = "North")
wc_out3_no <- make_twfe_event_study(regional_df%>% filter(region == "north"), govt_black_x_native_born, black_x_native_born, 'occ == "white_collar"', lab1 = "Black Native Born", lab2 = "White Collar") %>% mutate(reg = "North")

wc_out1_so <- make_twfe_event_study(regional_df %>% filter(region == "south"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "white_collar"', lab1 = "White Foreign Born", lab2 = "White Collar") %>% mutate(reg = "South")
wc_out2_so <- make_twfe_event_study(regional_df%>% filter(region == "south"), govt_white_x_native_born, white_x_native_born, 'occ == "white_collar"', lab1 = "White Native Born", lab2 = "White Collar") %>% mutate(reg = "South")
wc_out3_so <- make_twfe_event_study(regional_df%>% filter(region == "south"), govt_black_x_native_born, black_x_native_born, 'occ == "white_collar"', lab1 = "Black Native Born", lab2 = "White Collar") %>% mutate(reg = "South")

wc_out1_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "white_collar"', lab1 = "White Foreign Born", lab2 = "White Collar") %>% mutate(reg = "Midwest")
wc_out2_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_white_x_native_born, white_x_native_born, 'occ == "white_collar"', lab1 = "White Native Born", lab2 = "White Collar") %>% mutate(reg = "Midwest")
wc_out3_mw <- make_twfe_event_study(regional_df%>% filter(region == "midwest"), govt_black_x_native_born, black_x_native_born, 'occ == "white_collar"', lab1 = "Black Native Born", lab2 = "White Collar") %>% mutate(reg = "Midwest")

wc_out1_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "white_collar"', lab1 = "White Foreign Born", lab2 = "White Collar") %>% mutate(reg = "West")
wc_out2_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_white_x_native_born, white_x_native_born, 'occ == "white_collar"', lab1 = "White Native Born", lab2 = "White Collar") %>% mutate(reg = "West")
wc_out3_we <- make_twfe_event_study(regional_df%>% filter(region == "west"), govt_black_x_native_born, black_x_native_born, 'occ == "white_collar"', lab1 = "Black Native Born", lab2 = "White Collar") %>% mutate(reg = "West")



#blue collar plot

regional_plot_bc <-
  bind_rows(bc_out1_no, bc_out2_no, bc_out3_no,
          bc_out1_so, bc_out2_so, bc_out3_so,
          bc_out1_mw, bc_out2_mw, bc_out3_mw,
          bc_out1_we, bc_out2_we, bc_out3_we
) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(pre_post = (val > -1)*1) %>%
  mutate(reg = factor(reg, levels = c("North", "South", "Midwest", "West"))) %>%
  mutate(lab1 = factor(lab1, levels = c("White Foreign Born", "Black Native Born", "White Native Born"))) %>%
  ggplot(aes(x=val, y = estimate, shape = lab1, color = lab1, group = lab1, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  facet_wrap(reg~., ncol = 2) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "bottom",
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  theme(text=element_text(size=9)) +
  scale_y_continuous(limits = c(-0.25, 0.25))

#white collar plot

regional_plot_wc <-
  bind_rows(wc_out1_no, wc_out2_no, wc_out3_no,
            wc_out1_so, wc_out2_so, wc_out3_so,
            wc_out1_mw, wc_out2_mw, wc_out3_mw,
            wc_out1_we, wc_out2_we, wc_out3_we
  ) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(pre_post = (val > -1)*1) %>%
  mutate(reg = factor(reg, levels = c("North", "South", "Midwest", "West"))) %>%
  mutate(lab1 = factor(lab1, levels = c("White Foreign Born", "Black Native Born", "White Native Born"))) %>%
  ggplot(aes(x=val, y = estimate, shape = lab1, color = lab1, group = lab1, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  facet_wrap(reg~., ncol = 2) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "bottom",
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  theme(text=element_text(size=9)) +
  scale_y_continuous(limits = c(-0.25, 0.25))

#ggsave('../Apps/Overleaf/merit paper/outputs/bc_regional_plot_all.png', plot = regional_plot_bc, width=6, height=6)
#ggsave('../Apps/Overleaf/merit paper/outputs/wc_regional_plot_all.png', plot = regional_plot_wc, width=6, height=6)

ggsave('./replication_file/_5_outputs/figures/figure_a9.png', plot = regional_plot_bc, width=6, height=6)
ggsave('./replication_file/_5_outputs/figures/figure_a10.png', plot = regional_plot_wc, width=6, height=6)

